package com.pig.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig.mapper.UserMapper;
import com.pig.pojo.User;
import com.pig.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Calendar;
import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void doSaveUser(User user) {
        String password = user.getPassword();
        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(pwd);
        user.setCreated(new Date());
        user.setUpdated(new Date());
        userMapper.insert(user);
    }

    @Override
    public int checkUsername(String username) {
        QueryWrapper queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public int checkPhone(String phone) {
        QueryWrapper queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        return userMapper.selectCount(queryWrapper);
    }
}
